package DAO;

import java.util.List;
import metier.User;

public class DAOUserJPA extends DAOJPA implements DAOUser{

	private static DAOUserJPA instance=null;
	
	private DAOUserJPA() {}
	
	public static DAOUserJPA getInstance() {
		if(instance == null)
			instance = new DAOUserJPA();
		return instance;
	}

	
	@Override
	public User get(int id) {
		User utilisateur = DAOJPA.getManager().find(User.class, id);
		return utilisateur;
	}

	@Override
	public void save(User utilisateur) {
		if(utilisateur.getId() != -1)
			throw new IllegalArgumentException("utilisateur d�ja enregistr�");
		
		DAOJPA.getManager().persist(utilisateur);
		DAOJPA.commit();
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> trouver(String login, String mdp) {
		return DAOJPA.getManager().createNativeQuery("SELECT * FROM user u WHERE u.login ='"+login+"' AND u.mdp = '"+mdp+"'",User.class).getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public List<User> trouverLogin (String login){
		return DAOJPA.getManager().createNativeQuery("SELECT * FROM user u WHERE u.login ='"+login+"'",User.class).getResultList();
	}

}
